<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">



        <p-form-item label="物料编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'code', validatorRules.code]" placeholder="请输入物料编号"></p-input>
        </p-form-item>

        <p-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'name', validatorRules.name]" placeholder="请输入物料名称"></p-input>
        </p-form-item>

        <p-form-item label="物料描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'comment', validatorRules.comment]" placeholder="请输入物料描述"></p-input>
        </p-form-item>

        <p-form-item label="物料规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'size', validatorRules.size]" placeholder="请输入物料规格"></p-input>
        </p-form-item>

        <p-form-item label="物料状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['status', validatorRules.status]" :trigger-change="true" dictCode="mat_status" placeholder="请选择物料状态"/>
        </p-form-item>

        <p-form-item label="数据软删除标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['flag', validatorRules.flag]" :trigger-change="true" dictCode="mat_flag" placeholder="请选择数据软删除标识"/>
        </p-form-item>

        <p-form-item label="替代物料标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'substituteCode', validatorRules.substituteCode]" placeholder="请输入替代物料标识"></p-input>
        </p-form-item>

        <p-form-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['type', validatorRules.type]" :trigger-change="true" dictCode="mat_type" placeholder="请选择物料类型"/>
        </p-form-item>

        <p-form-item label="物料类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['categoryId', validatorRules.categoryId]" :trigger-change="true" dictCode="mat_category_id" placeholder="请选择物料类别"/>
        </p-form-item>

        <p-form-item label="物料组标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'groupId', validatorRules.groupId]" placeholder="请输入物料组标识"></p-input>
        </p-form-item>

        <p-form-item label="物料版本" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'version', validatorRules.version]" placeholder="请输入物料版本"></p-input>
        </p-form-item>

        <p-form-item label="ERP仓库" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'warehouseErp', validatorRules.warehouseErp]" placeholder="请输入ERP仓库"></p-input>
        </p-form-item>

        <p-form-item label="更新人标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'updatetimeBy', validatorRules.updatetimeBy]" placeholder="请输入更新人标识"></p-input>
        </p-form-item>

        <p-form-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择更新时间" v-decorator="[ 'updatetimeTime', validatorRules.updatetimeTime]" :trigger-change="true" style="width: 100%"/>
        </p-form-item>

        <p-form-item label="时间标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择时间标识" v-decorator="[ 'timestamp', validatorRules.timestamp]" :trigger-change="true" style="width: 100%"/>
        </p-form-item>

        <p-form-item label="公司标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'companyId', validatorRules.companyId]" placeholder="请输入公司标识"></p-input>
        </p-form-item>

        <p-form-item label="工厂标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'factoryId', validatorRules.factoryId]" placeholder="请输入工厂标识"></p-input>
        </p-form-item>

        <p-form-item label="租户标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'clientId', validatorRules.clientId]" placeholder="请输入租户标识"></p-input>
        </p-form-item>

        <p-form-item label="替换人员标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'substituteId', validatorRules.substituteId]" placeholder="请输入替换人员标识"></p-input>
        </p-form-item>


      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'

  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "MaterialMainModal",
    components: { 
      JDate,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          code: {rules: [
          ]},
          name: {rules: [
            {required: true, message: '请输入物料名称!'},
          ]},
          comment: {rules: [
          ]},
          size: {rules: [
          ]},
          status: {rules: [
          ]},
          flag: {rules: [
          ]},
          substituteCode: {rules: [
          ]},
          type: {rules: [
          ]},
          categoryId: {rules: [
          ]},
          groupId: {rules: [
          ]},
          version: {rules: [
          ]},
          warehouseErp: {rules: [
          ]},
          updatetimeBy: {rules: [
          ]},
          updatetimeTime: {rules: [
          ]},
          timestamp: {rules: [
          ]},
          companyId: {rules: [
          ]},
          factoryId: {rules: [
          ]},
          clientId: {rules: [
          ]},
          substituteId: {rules: [
          ]},
        },
        url: {
          add: '/basic-business/basicMaterialMain/materialMain/add',
          edit: '/basic-business/basicMaterialMain/materialMain/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },

      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'code','name','comment','size','status','flag','substituteCode','type','categoryId','groupId','version','warehouseErp','updatetimeBy','updatetimeTime','timestamp','companyId','factoryId','clientId','substituteId'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.msg);
                that.$emit('ok');
              }else{
                that.$message.warning(res.msg);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'code','name','comment','size','status','flag','substituteCode','type','categoryId','groupId','version','warehouseErp','updatetimeBy','updatetimeTime','timestamp','companyId','factoryId','clientId','substituteId'))
      },

      
    }
  }
</script>